package Igor_Dominiak;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Macierz {
	private int w, k;
	private LiczbaZespolona macierz[][];

	public Macierz(int w, int k) {
		this.w = w;
		this.k = k;
		macierz = new LiczbaZespolona[w][k];
	}

	public int getW() {
		return w;
	}

	public void setW(int w) {
		this.w = w;
	}

	public int getK() {
		return k;
	}

	public void setK(int k) {
		this.k = k;
	}

	public LiczbaZespolona[][] getMacierz() {
		return macierz;
	}

	public void setMacierz(LiczbaZespolona[][] macierz) {
		this.macierz = macierz;
	}

	public static void wypelnij(Macierz m) {
		LiczbaZespolona[][] macierz = m.getMacierz();
		Random r = new Random();
		for (int i = 0; i < m.getW(); i++) {
			for (int j = 0; j < m.getK(); j++) {
				LiczbaZespolona z = new LiczbaZespolona(
						(r.nextDouble() * 200) - 100,
						(r.nextDouble() * 200) - 100);
				macierz[i][j] = z;
			}
		}
		m.setMacierz(macierz);
	}

	public static void suma(Macierz m1, Macierz m2) {
		LiczbaZespolona[][] macierz1 = m1.getMacierz();
		LiczbaZespolona[][] macierz2 = m2.getMacierz();
		Macierz macierz3 = new Macierz(m1.getW(), m2.getK());
		LiczbaZespolona[][] suma = macierz3.getMacierz();
		if ((m1.getW() == m2.getW()) && m1.getK() == m2.getK()) {
			for (int i = 0; i < macierz3.getW(); i++) {
				for (int j = 0; j < macierz3.getK(); j++) {
					LiczbaZespolona z = new LiczbaZespolona();
					suma[i][j] = z.suma(macierz1[i][j], macierz2[i][j]);
				}
			}
		} else
			System.out.println("Macierze nie sa tego samego stopnia!");
		for (int i = 0; i < macierz3.getW(); i++) {
			for (int j = 0; j < macierz3.getK(); j++) {
				if (suma[i][j].getIm() >= 0) {
					System.out.print(suma[i][j].getRe() + " + "
							+ suma[i][j].getIm() + "i   ");
				} else {
					System.out.print(suma[i][j].getRe() + " "
							+ suma[i][j].getIm() + "i   ");
				}
			}
			System.out.println();
		}
	}

	public static void iloczyn(Macierz m1, Macierz m2) {
		LiczbaZespolona[][] macierz1 = m1.getMacierz();
		LiczbaZespolona[][] macierz2 = m2.getMacierz();
		Macierz macierz3 = new Macierz(m1.getW(), m2.getK());
		LiczbaZespolona[][] iloczyn = macierz3.getMacierz();
		List<LiczbaZespolona> list = new ArrayList<LiczbaZespolona>();
		LiczbaZespolona z = new LiczbaZespolona();
		if (m1.getK() == m2.getW()) {
			for (int i = 0; i < macierz3.getW(); i++) {
				for (int j = 0; j < macierz3.getK(); j++) {
					for (int k = 0; k < m1.getK(); k++) {
						list.add(z.iloczyn(macierz1[i][k], macierz2[k][j]));
					}
					for (int l = 0; l < list.size(); l++) {
						if (l != 0) {
							iloczyn[i][j] = z.suma(iloczyn[i][j], list.get(l));
						} else
							iloczyn[i][j] = list.get(0);
					}
					list.clear();
				}
			}
		} else
			System.out.println("Macierzy nie mo�na wymno�y�!");
		for (int i = 0; i < macierz3.getW(); i++) {
			for (int j = 0; j < macierz3.getK(); j++) {
				if (iloczyn[i][j].getIm() >= 0) {
					System.out.print(iloczyn[i][j].getRe() + " + "
							+ iloczyn[i][j].getIm() + "i   ");
				} else {
					System.out.print(iloczyn[i][j].getRe() + " "
							+ iloczyn[i][j].getIm() + "i   ");
				}
			}
			System.out.println();
		}
	}

	public static void main(String[] args) {
		Macierz m1 = new Macierz(3, 3);
		wypelnij(m1);
		Macierz m2 = new Macierz(3, 3);
		wypelnij(m2);
		suma(m1, m2);
		System.out.println();
		iloczyn(m1, m2);
	}
}
